package com.fruit.service.promotion.impl;

import com.joe.utils.cluster.ClusterManager;
import com.joe.utils.cluster.redis.RedisBaseConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.Lock;

/**
 * 锁管理器
 *
 * @author joe
 * @version 2017.12.12 10:06
 */
@Service
@Slf4j
public class LockManager {
    @Autowired
    private RedisBaseConfig redisBaseConfig;
    @Autowired
    private ClusterManager clusterManager;

    /**
     * 获取分布式锁
     *
     * @param lockName 锁名称
     * @return 获取成功返回锁，获取失败返回null
     */
    public Lock getLock(String lockName) {
        if (clusterManager != null) {
            return clusterManager.getLock(lockName);
        } else {
            log.warn("获取不到锁，当前redis配置是：[{}]", redisBaseConfig);
            return null;
        }
    }
}
